<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <style>
    img {
      width: 300px;
      display: block;
    }
  </style>
  <img src="./loading.gif" alt="" data-src="./1.webp">
  <img src="./loading.gif" alt="" data-src="./2.webp">
  <img src="./loading.gif" alt="" data-src="./3.webp">
  <img src="./loading.gif" alt="" data-src="./4.webp">
  <img src="./loading.gif" alt="" data-src="./1.webp">
  <img src="./loading.gif" alt="" data-src="./2.webp">
  <img src="./loading.gif" alt="" data-src="./3.webp">
  <img src="./loading.gif" alt="" data-src="./4.webp">
  <img src="./loading.gif" alt="" data-src="./1.webp">
  <img src="./loading.gif" alt="" data-src="./2.webp">
  <img src="./loading.gif" alt="" data-src="./3.webp">
  <img src="./loading.gif" alt="" data-src="./4.webp">
  <img src="./loading.gif" alt="" data-src="./1.webp">
  <img src="./loading.gif" alt="" data-src="./2.webp">
  <img src="./loading.gif" alt="" data-src="./3.webp">
  <img src="./loading.gif" alt="" data-src="./4.webp">
  <img src="./loading.gif" alt="" data-src="./1.webp">
  <img src="./loading.gif" alt="" data-src="./2.webp">
  <img src="./loading.gif" alt="" data-src="./3.webp">
  <img src="./loading.gif" alt="" data-src="./4.webp">
  <script>
    var imgs = document.querySelectorAll('img');
    function lozyLoad() {
      var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
      var winHeight = window.innerHeight;
      for (let i = 0; i < imgs.length; i++) {
        if (imgs[i].offsetTop < scrollTop + winHeight) {
          imgs[i].src = imgs[i].getAttribute('data-src');
        }
      }
    }
    window.addEventListener("load", lozyLoad) //加载时候可视区域要为真实图片
    window.addEventListener("scroll", lozyLoad)
  </script>
</body>

</html>